package com.eptok.gw.szcups.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class SerialNumber {
	
	public static int chanTrackingNo;			    //渠道系统跟踪号
	public static int activeMerTrackingNo;			//商户系统跟踪号
	public static int chanRetrievalNumber;			    //渠道检索参考号
	public static int activeRetrievalNumber;			//商户检索参考号
	
	private static final SimpleDateFormat yyMMddHHmmss = new SimpleDateFormat("yyMMddHHmmss");
	private static final SimpleDateFormat MMddHHmmss = new SimpleDateFormat("MMddHHmmss");
	private static int seq;

	/**
	 * 获取000001--999999的渠道循环流水号[系统跟踪号]
	 */
	public static synchronized String getChanSequence(){
		if(chanTrackingNo == 999999){
			chanTrackingNo = 1;
		}else{
			chanTrackingNo++;
		}
		return String.format("%06d", chanTrackingNo);
	}
	
	
	public static synchronized String getChanRetrievalNumber(){
		final StringBuilder buf = new StringBuilder(12);
		buf.append(MMddHHmmss.format(new Date()));
		if(chanRetrievalNumber == 99){
			chanRetrievalNumber = 1;
		}else{
			chanRetrievalNumber++;
		}
		buf.append(String.format("%02d", chanRetrievalNumber));
		return buf.toString();
	}
	
	
	/**
	 * 获取000001--999999的商户[银盛主动]循环流水号[系统跟踪号]
	 */
	public static synchronized String getMerchanSequence(){
		if(activeMerTrackingNo == 999999){
			activeMerTrackingNo = 1;
		}else{
			activeMerTrackingNo++;
		}
		return String.format("%06d", activeMerTrackingNo);
	}

	
	public static synchronized String getActiveRetrievalNumber(){
		final StringBuilder buf = new StringBuilder(12);
		buf.append(MMddHHmmss.format(new Date()));
		if(activeRetrievalNumber == 99){
			activeRetrievalNumber = 1;
		}else{
			activeRetrievalNumber++;
		}
		buf.append(String.format("%02d", activeRetrievalNumber));
		return buf.toString();
	}
	
	
	/**
	 * 生成一个直充交易的请求流水 D + yyMMddHHmmss + xxx
	 * 
	 * @return
	 */
	public static synchronized String seqno() {
		final StringBuilder buf = new StringBuilder(16);
		buf.append("S");
		buf.append(yyMMddHHmmss.format(new Date()));
		if(seq == 999){
			seq = 1;
		}else{
			seq++;
		}
		buf.append(String.format("%03d", seq));
		return buf.toString();
	}
	
	public static void main(String[] args){
		java.util.Calendar  cal = Calendar.getInstance();
		cal.set(1970, 1, 1 ,0, 0, 0);
		java.util.Calendar  cal2 = Calendar.getInstance();
		cal2.set(2014, 1, 1);
			System.out.println(cal2.getTimeInMillis()-cal.getTimeInMillis());
			System.out.println(getActiveRetrievalNumber());
			//1365497303519
			//C65497316080
	}

}
